<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="分类名称" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="pid">
        <el-select v-model="form.pid" clearable filterable>
          <el-option
            label="顶级分类"
            value="0"
          />
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类排序" prop="sort">
        <el-input-number :min="0" v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import categoryApi from "@/api/category";
export default {
  name: "categorySave",
  props: {
    categoryId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      options: [],
      form: {
        id: "",
        title: "",
        pid: "",
        sort: 0,
      },
      rules: {
        title: [{ required: true, message: "分类名称不能为空", trigger: "blur" }],
        pid: [{ required: true, message: "所属分类不能为空", trigger: "change" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form = {
        id: "",
        title: "",
        pid: "",
        sort: 0
      };
    },
    categoryId(newValue, oldValue){
      if (this.categoryId) this.getInfo(this.categoryId);
    }
  },
  created() {
    if (this.categoryId) this.getInfo(this.categoryId);
  },
  methods: {
    // 获取数据
    async getInfo(id) {
      let res = await categoryApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.categoryId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await categoryApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await categoryApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>